<?php

namespace App\Http\Controllers\Admin\Advertisement;
use App\Http\Controllers\Admin\BaseController;
use Illuminate\Http\Request;

class AdvertisingListController extends BaseController
{
    protected $api_url = [
        'index' => ['get', 'advertisinglist'],
        'show'  => ['get', 'advertisinglist/%d'],
        'update'  => ['put', 'advertisinglist/%d'],
        'store'  => ['post', 'advertisinglist'],
        'delete'=> ['delete', 'advertisinglist/%d'],
        'audit'  => ['put', 'advertisinglist/audit/%d'],
        'category'  => ['get', 'advertisinglist/advertisingposition'],
        'categoryfid'  => ['post', 'advertisinglist/categoryfid/%id'],
        'cates' => ['get', 'product/cate'],
    ];

    protected $request;

    public function __construct(Request $request)
    {
        $this->request = $request;
    }
    /**
     * 后台-文章列表
     * */
    public function index(){

        //参数部分
        $search_params = $this->request->all('position');
        $page = $this->request->get('page', 1);
        $params = [
            'search' => search_params($search_params),
            'searchJoin' => 'and',
            'page' => $page
        ];

        $category = http_admin($this->api_url['category'])->send();

        //请求部分
        $ret = http_admin($this->api_url['index'])->send($params);
        //结果处理部分
        $data = paginate($this->request, $ret['data']);

        //回填搜索框值
        $data['search_params'] = $search_params;
        $data['category']=$category['data'];
        $data['search_params'] = $search_params;

        return view('Admin.advertisement.article', $data);
    }

    public function create(){
        $advertisement_position = http_admin($this->api_url['category'])->send();
        $category = http_admin($this->api_url['cates'])->send();
        $ret['category'] = $category['data']['list'];
        $ret['advertisement_position']=$advertisement_position['data'];
        return view('Admin.advertisement.article_create', $ret);

    }

    public function store(){
        $data=$this->request->all();
        $ret = http_admin($this->api_url['store'])->send($data);
        $this->reSetWebIndexRedis(['data','shop_data']); // 清空重置前台首页 redis
        return response()->json(['code'=>0,'message'=>'操作成功','data'=>$ret['data']]);

    }


/*    public function show($id){
        //请求部分
        $ret = http_admin($this->api_url['show'],[$id])->send();

        $category = http_admin($this->api_url['category'])->send();
        $ret['data']['category_pid']=$category['data']['pid'];
        $ret['data']['category_fid']=$category['data']['fid'];
        return view('Admin.advertisement.article_edit', $ret['data']);

    }*/

    /**
     * 后台-编辑页面
     * */
    public function edit($id){
        $info = http_admin($this->api_url['show'], [$id])->send();
        $advertisement_position = http_admin($this->api_url['category'])->send();
        $category = http_admin($this->api_url['cates'])->send();

        $ret['data'] = $info['data']['data'];
        $ret['category'] = $category['data']['list'];
        $ret['advertisement_position']=$advertisement_position['data'];
        return view('Admin.advertisement.article_edit', $ret);
    }


    public function update($id){
        $data=$this->request->all();
        $ret = http_admin($this->api_url['update'],[$id])->send($data);
        $this->reSetWebIndexRedis(['data','shop_data']); // 清空重置前台首页 redis

        return response()->json(['code'=>0,'message'=>'操作成功','data'=>$ret['data']]);

    }


    /**
     * 后台-删除
     * */
    public function destroy($id){
        $ret = http_admin($this->api_url['delete'],[$id])->send();
        return redirect('/foradmin/advertisinglist');
    }


    /**
     * 展示
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function audit($id){
        //请求部分
        $data=$this->request->all(['audit']);
        $ret = http_admin($this->api_url['audit'],[$id])->send($data);
        return response()->json(['code'=>0,'message'=>'操作成功','data'=>$ret['data']]);
    }



}